/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-08-25 13:07:58
 * @filename : static/nmm/src/views/favorites.js
 * @version : 
 * @description : 
 */

define(function(require, exports, module){
    require("{component}dialog/zepto.dialog.css");
    require('{component}promptTip/promptTip.css');
    var favoritesView, scrollIAjax = true, scrollFAjax=true, userInfo, tab='myFavorites',
        mId = 'favorites',reRender = true,menuName = 'favorites',
        scrollTop = {},
        conditionF = {},
        conditionI = {},
        panel = require('{component}panel/zepto.panel'),
        dialog = require('{component}dialog/zepto.dialog'),
        promptTip = require('{component}promptTip/promptTip'),
        imglazyload = require('{component}imglazyload/zepto.imglazyload'),
        tpl = require('{view}favorites.tpl'),
        tplL = require('{view}userListLi.tpl'),
        tplN = require('{view}userNav.tpl');

    favoritesView = Backbone.View.extend({

        el: 'body',

        template: _.template(tpl),
        templateN: _.template(tplN),
        templateL: _.template(tplL),

        initialize: function () {},
        
        render: function (obj) {
            if (!share.checkPermission(true)){return;};
            share.reRender(reRender,mId);
 
            if ($('#'+mId).length < 1) {
                $('body').append(this.template(lang));
                this.defaultData();
                this.bindEvt();
                userInfo = share.userInfo();
            } else {
                $('#'+mId).css('display','block');
            }
            share.panel(panel,this.templateN,mId,menuName);
            this.tabSwitch();
        },

        defaultData : function() {
            scrollTop = {
               myFavorites : 0,
               interestedInMe : 0
            };
            conditionF = {
                page_num : 0,
                offset : 20
            };
            conditionI = {
                page_num :0,
                offset : 20
            };
        },   

        bindEvt : function() {
            var tthis = this,dom = $('#'+mId);
            $(window).on('scroll.favorites',function(e){
                if(seajs.moduleUI == 'favorites') {
                    var WH = $(window).height(),
                        SH = $(window).scrollTop(),
                        //zetpo position:absolte $(document).height() bug /jquery OK
                        DH = $('#'+mId).height();
                        scrollTop[tab] = SH;
                    if ( SH > 0 && (DH-WH-SH) < 100) { 
                        tthis.ajaxList();
                        //WH = $(window).height();
                        //window.scroll(0,WH);
                    }
                    dom.find('#'+tab).imglazyload({
                        startY : SH,
                        endY:SH + $(window).height()
                    });
                }
            });

            $('#backTop').live('tap.favorites',function(){
                window.scroll(0,0); 
                share.backTopEnd($('body'));
            });

            dom.find('.left').on('tap',function(e){
                e.stopPropagation();
                if (panel.info.state == 'hidden') {
                    panel.show();
                } else {
                    panel.hidden(); 
                }
            });

            dom.find('.userList').on('touchstart',function(e) {
                $(this).find('li').css('background','transparent');           
                var li = $(e.target).closest('li');
                li.css('background','#D5DDE0');           
            }).on('touchend',function(e){
                var li = $(e.target).closest('li');
                li.css('background','transparent');           
            });

            dom.on('tap',function(){
                if (panel.info.state == 'show' && $(window).width() < 640) {
                    panel.hidden();
                }
            });

            dom.find('.fav_topNav').find('li').on('tap',function(){
                $(this).parent().find('li').removeClass('action');
                $(this).addClass('action');
                tab = $(this).attr('ids');
                tthis.tabSwitch();  
            });

            dom.find('.userList').on('tap',function(e) {
                var uid = $(e.target).closest('li').attr('uid'),
                    userName = $(e.target).closest('li').attr('username');
                if (uid) {
                    if ($(window).height() < 640) {
                        if (panel.info.state == 'hidden') {
                            window.location.href = '#userProfile/whole/custom'+uid+'&&'+userName; 
                        }
                    } else {
                        window.location.href = '#userProfile/whole/custom'+uid+'&&'+userName; 
                    }
                }
            });
        },

        tabSwitch : function() {
            $('#'+mId).find('.userList').css('display','none');
            $('#'+mId).find('#'+tab).css('display','block');
            var dom = $('#'+mId).find('#'+tab);
            if (dom.find('li').length < 1) {
                this.ajaxList();
            } else {
                (function(y){
                    setTimeout(function() {
                       window.scroll(0,y);
                    },10);
                })(scrollTop[tab]);
            }
        },
        /*
        closeNoProfiles : function(dom) {
            dialog.close({dom:dom});
        },

        noProfiles: function(dom) {
            dialog.open({
                type    : 'tipDialog',
                content : lang.noData
            });
        },
        */
        ajaxList : function() {
            if (tab == 'myFavorites') {
                this.ajaxListF(conditionF,tab);
            } else {
                if (share.isGuest()) {
                    dialog.open({
                        type : 'payDialog',
                        dom : $('#'+mId).find('#interestedInMe'),
                        cover : true,
                        positionRelative : true
                    }); 
                } else {
                    this.ajaxListI(conditionI,tab);
                }
            }
        },
 
        ajaxListF : function(data,tab) {
            var tthis = this , dom = $('#'+mId).find('#'+tab);
            var obj = {
                url : seajs.data.vars.apiAccessUrl+'my_favors',
                type : 'POST',
                data : data,
                success : function(data) {
                    var list = data.res;
                    if (list.length == 0 && conditionF['page_num']==0) {
                         tthis.noData(dom,'When your add someone to your favorites, they\'ll show up here.');
                         return;
                    } else {
                        var Img = seajs.data.vars.resourceUrl+'img/';
                        $('#'+mId).find('#'+tab).append(tthis.templateL({arr:data.res,defaultImg:Img}));
                        dom.imglazyload({startY : 0,endY: $('#'+mId).height()});
                        conditionF['page_num']++;
                        //share.cacheUserList(data.res);
                    }
                },
                error : function(xhr, errorType, error) { 
                },
                complete : function(xhr, status) {
                    share.ajaxEndB($('#'+mId).find('ul'));
                    setTimeout(function(){
                        scrollFAjax = true;
                    },500)
                }
            };

            if (scrollFAjax) {
                promptTip.hide({type:'noData', dom:dom});
                scrollFAjax = false;
                share.ajaxStartB(dom);
                $.ajax(share.ajaxControl(obj));
            }
        },

        ajaxListI : function(data,tab) {
            var tthis = this , dom = $('#'+mId).find('#'+tab);
            var obj = {
                url : seajs.data.vars.apiAccessUrl+'interested_in_me',
                type : 'POST',
                data : data,
                success : function(data) {
                    var list = data.res;
                    if (list.length == 0 && conditionI['page_num']==0) {
                        tthis.noData(dom,'When someone add you to thier favorites, they\'ll show up here.');
                        return;
                    } else {
                        var Img = seajs.data.vars.resourceUrl+'img/';
                        $('#'+mId).find('#'+tab).append(tthis.templateL({arr:data.res,defaultImg:Img}));
                        dom.imglazyload({startY : 0,endY: $('#'+mId).height()});
                        conditionI['page_num']++;
                        //share.cacheUserList(data.res);
                    }
                },
                error : function(xhr, errorType, error) { 
                },
                complete : function(xhr, status) {
                    share.ajaxEndB($('#'+mId).find('ul'));
                    setTimeout(function(){
                        scrollIAjax = true;
                    },500)
                }
            };

            if (scrollIAjax) {
                promptTip.hide({type:'noData', dom:dom});
                scrollIAjax = false;
                share.ajaxStartB(dom);
                $.ajax(share.ajaxControl(obj));
            }
        },

        noData : function(dom,content) {
            promptTip.show({type:'noData',text:content, dom:dom});
        }

    });

    module.exports = favoritesView;
})

